package net.oschina.simpleplus;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.oschina.simpleplus.cnf.GlobalConfig;
import net.oschina.simpleplus.core.SnowFlake;
import net.oschina.simpleplus.tcp.TcpServer;

public class StartUpServer {
	 private static final Logger logger = LoggerFactory.getLogger(StartUpServer.class);
	
	public static void main(String[] args) {
		 int port = GlobalConfig.DEFAULT_PORT;
		 long datacenterId = GlobalConfig.DATACENTER_ID;
	     long machineId = GlobalConfig.MACHINES_SIGN;

	     if (args != null && args.length == 3) {
	    	 port = Integer.valueOf(args[0]);
	         datacenterId = Long.valueOf(args[1]);
	         machineId = Long.valueOf(args[2]);
	     }else{
	    	 logger.info(">>>>>You don't appoint the port datacenterId and machineId argement,will use default value");
	     }
	     
	     final SnowFlake snowFlake = new SnowFlake(datacenterId, machineId);
	     
	     // 启动Sdk服务器
        final TcpServer tcpServer = new TcpServer(snowFlake);
        tcpServer.setPort(port);
        tcpServer.init();
        tcpServer.start();

        logger.info(">>>>>Server start success, SnowFlake datacenterId is {}, machineId is {}",
                datacenterId,
                machineId
        );

        // TODO 雪花算法数据中心标识、机器标识、服务IP、服务端口上报到配置中心

        // 注册进程钩子，在JVM进程关闭前释放资源
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                tcpServer.shutdown();
                logger.info(">>>>>>>>>> sdkServer shutdown");
                System.exit(0);
            }
        });
		
	}
}
